<?php
$loader->requireOnce("includes/Datasource_sql.class.php");

class PatientOtherDisease extends ORDataObject {
	var $patient_other_disease_id 	= 	'';
	var $patient_id 				= 	'';
	var $disease_name				=	'';
	var $date_admitted				=	'';
	var $updated 					= 	'';
	var $note 						= 	'';
	var $status						= 	'';
	
	var $_table = 'patient_other_disease';
	var $_key = 'patient_other_disease_id';
	var $_internalName = 'PatientOtherDisease';
	var $_foreignKeyList=array(
		'patient_id'=>"patient"
	);
	
	const OTHDIS_STATUS_INSTREATMENT	=	'in_treatment';
	const OTHDIS_STATUS_RECOVERED		=	'recovered';
	
	function PatientOtherDisease($db='') {
		parent::ORDataObject($db);
	}
	
	public function getOtherDiseaseStatusList(){
		$list	=	Array(
			self::OTHDIS_STATUS_INSTREATMENT	=>	L10nHelper::_(self::OTHDIS_STATUS_INSTREATMENT),
			self::OTHDIS_STATUS_RECOVERED		=>	L10nHelper::_(self::OTHDIS_STATUS_RECOVERED)
		);
			
        return $list;
	}
	
	function getPatientOtherDisease($patient_id){
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "patient_other_disease_id as id, disease_name, date_admitted,
 							note, status, updated",
			'from' 		=> "$this->_table",
			'where' 	=> "patient_id = ".(int)$patient_id
			);
					
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	
	public function checkDuplicated($patientid,$disease_name,$admitted,$status){
		$ds =& new Datasource_sql();
        $sql = array(
            'cols'     => "patient_other_disease_id  as id",
            'from'     => "$this->_table",
        	'where'		=> "patient_id=$patientid AND disease_name='$disease_name'".
        			" AND date_admitted='$admitted' AND status='$status'"
            );
        $ds->setup($this->_db,$sql);
        $res=$ds->toArray();
      	if(!empty($res))
      		return $res[0]['id'];
      	return 0;
	}
	
	
	public function setData($patientid,$disease_name,$admitted,$status,$note,$updated=''){
		if($updated!='')
			$date=$updated;
		else
			$date=date('Y-m-d');
		$data = array (
				'patient_id' 		=> 	$patientid,
				'disease_name'		=> 	$disease_name,
		        'date_admitted'     => 	$admitted,
				'note'				=> 	$note,
				'status'			=>	$status,
				'updated'			=>	$date
			);		
		$this->populateArray($data);
	}
	
	
	public function getUpdated(){
		return $this->getLocalizedDate('updated');
	}
	
	public function getAdmitted(){
        return $this->getLocalizedDate('date_admitted');
	}
	
	public function getStatus(){
		$list	=	self::getOtherDiseaseStatusList();
		if(!array_key_exists($this->status, $list)){
			return '';
		}
		return $list[$this->status];
	}
	
	public function getNote(){
		return $this->note;
	}
	
	public function getDiseaseName(){
		return $this->disease_name;
	}
	
	public function deleteOtherDisease($id){
		$sql	=	"DELETE from  $this->_table where $this->_key=".(int)$id;
		$this->_execute($sql);
	}
	
	public function setId($id){
		$this->patient_other_disease_id	=	$id;
	}
	
	public function getId(){
		return $this->patient_other_disease_id;
	}
}
?>